package com.Shop.Center.controller;

import com.Shop.Center.entity.Items;
import com.Shop.Center.service.AdminLoginService;
import com.Shop.Center.service.AdminUserService;
import com.Shop.Center.service.ItemsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class ItemsController {

    @Resource
    private ItemsService itemsService;
    @Resource
    private AdminLoginService adminLoginService;

    @ResponseBody
    @PostMapping("/login_items")
    public Boolean runLogin(@RequestParam("name") String name, @RequestParam("password") String password){
        return adminLoginService.user_login(name, password);
    }

    @ResponseBody
    @GetMapping("/items")
    public List<Items> getItems(){
        return itemsService.getAllItems();
    }

    @ResponseBody
    @PostMapping("/items/add")
    public int addItem(@RequestParam("name") String name, @RequestParam("info")String info,
                       @RequestParam("originPrice") Double originPrice, @RequestParam("sellingPrice")Double sellingPrice,
                       @RequestParam("amount") Integer amount, @RequestParam("sellStatus")char sellStatus,
                       @RequestParam("imageUrl") String imageUrl, @RequestParam("type")String type,
                       @RequestParam("tag") String tag){
        if(AdminLoginService.is_login)
            return itemsService.addItem(name,info,originPrice,sellingPrice,amount,sellStatus,imageUrl,type,tag);
        return -1;
    }

    @ResponseBody
    @PostMapping("/items/update")
    public int updateItem(@RequestBody Items item) {
        if (Objects.isNull(item.getId()) || StringUtils.isEmpty(item.getName())||!AdminLoginService.is_login) {
            return -1;
        }
        return itemsService.updateInfo(item);
    }
    //{ "id":1,"sellingPrice":420,"name":"wdm" }

    @ResponseBody
    @PostMapping("/items/status/{sellStatus}")
    public int updateSellStatus(@RequestBody String json, @PathVariable("sellStatus") char sellStatus) {
        Long id = Long.parseLong(JSON.parseObject(json).get("id").toString());
        if(AdminLoginService.is_login)
            return itemsService.updateSellStatus(id,sellStatus);
        return -1;
    }
    //{ "id":"1" }

    @ResponseBody
    @DeleteMapping("/items/delete/{id}")
    public int deleteItem(@PathVariable("id") Long id) {
        if(AdminLoginService.is_login)
            return itemsService.deleteItem(id);
        return -1;
    }
    // delete/2

    @ResponseBody
    @PostMapping("/items/{type}")
    public List<Items> getTypeItems(@PathVariable("type") String type) {
        if(AdminLoginService.is_login)
            return itemsService.getTypeItems(type);
        return null;
    }

}
